<?php
/**
 * @author 
 * @copyright 2014
 */
 
 
class KlgbModel extends CI_Model
{
    var $_menus = array();
    
	function __construct(){
		parent::__construct('klgb');
	}
	
	function runsql($sql) {
		return $this->model->query($sql)->execute();
	}
	
	function tab_load_sql($sql) {
		$tong = 0;
		$res = $this->model->query($sql)->execute();
		if(count($res>0))
			foreach($res as $r)
				$tong=$r->tong;
		return $tong;
	}	
	
    function tinh_trang($ngay_hien_tai, $ngay_hoan_thanh, $ngay_canh_bao) {
		$tinh_trang = "";
		$mau="";	
		$hieu_ngay = $ngay_hoan_thanh-$ngay_hien_tai;
		if($hieu_ngay < 0)//do
		{
			$tinh_trang = 1;
			$mau = "background-color:#F00;";				
		}
		elseif($hieu_ngay <= $ngay_canh_bao)//vang
		{
			$tinh_trang = 2;
			$mau = "background-color:#FF0;";
		}
		else//xanh
		{
			$tinh_trang = 0;
			$mau = "background-color:#0CF;";
		}
        $kq = array();
        $kq['status'] = $tinh_trang;
        $kq['color']  = $mau;
		return $kq;
	}	
    
	function tinhtrang(){
		$sql = "
				SELECT t.id, t.`names`,
				(
					select count(1) tt
					from klgb k
					where EXISTS (
						select kl.idkl
						from klphongban kl
						where k.id = kl.idkl
						#and k.idphongban = ''
					)
				) as 'btd',
				(
					select count(1) tt
					from klgb k
					where (k.noidung is null or k.noidung = '')
				) as 'no',
				(
					select count(1) tt
					from klgb k
					where (UNIX_TIMESTAMP(k.ngayhoanthanh) >=  UNIX_TIMESTAMP(NOW())+(3*86400))
				) as 'th',
				(
					select count(1) tt
					from klgb k
					where (UNIX_TIMESTAMP(k.ngayhoanthanh) -  UNIX_TIMESTAMP(NOW())) > 0 
					and  (UNIX_TIMESTAMP(k.ngayhoanthanh) -  UNIX_TIMESTAMP(NOW())) < (3*86400)
				) as 'sth',
				(
					select count(1) tt
					from klgb k
					where (UNIX_TIMESTAMP(NOW()) > UNIX_TIMESTAMP(k.ngayhoanthanh)) 
				) as 'qh'
				from tabs t
				where 1 = 1
				limit 1
		";
		echo $sql; exit();
		$query = $this->model->query($sql)->execute();
		return isset($query[0])?$query[0]:"";
	}
		
	function getList($search,$loai,$login){
		$groups = $login['groups'];
		$donvi = $login['donvi'];
		$taikhoan = $login['taikhoan'];
		$taikhoanid = $login['id'];
		$sql = "
				SELECT kl.*, lgb.names as loaigiaoban, lkl.`names` as loaiketluan, kl.`pheduyet` as idpd2,pd.`names` as pheduyet
				FROM klgb kl				
				LEFT JOIN loaigiaoban lgb on lgb.id = kl.loaigiaoban
				LEFT JOIN hinhthuckls lkl on lkl.id = kl.loaikl
				LEFT JOIN pheduyet pd on pd.id = kl.pheduyet
				WHERE kl.isdelete = 0
				";
		if(isset($search['sqlbutton']) && !empty($search['sqlbutton'])) {
			$where = $search['sqlbutton'];
			$pos = strpos($where, 'WHERE');
			if ($pos !== false)
				$sql .= " AND ".substr($where,$pos+5);
		} else {
			if(!empty($search['id'])){
				$sql.= " AND kl.id like '%".$search['id']."%' ";
			}
			//if($taikhoan!="admin")
				//$sql .= " AND kl.pheduyet = 0 and kl.tpduyet = 0";
			
			if(!empty($search['maklgb'])){
				$sql.= " AND kl.id = '".$search['maklgb']."' ";
			}
			if(!empty($search['tungay'])){
				$sql.= " AND kl.ngaygiaoban >= '".date('Y-m-d',strtotime($search['tungay']))."' ";
			}
			if(!empty($search['denngay'])){
				$sql.= " AND kl.ngaygiaoban <= '".date('Y-m-d',strtotime($search['denngay']))."' ";
			}
			if(!empty($search['sovanban'])){
				$sql.= " AND kl.sovanban like '%".$search['sovanban']."%' ";
			}
			if(!empty($search['loaikl'])){
				$sql.= " AND kl.loaikl in (".$search['loaikl'].") ";
			}
			else{
				if($loai != ""){
					$sql.= " AND kl.loaikl = '".$loai."' ";
				}
			}
			if(!empty($search['chutri'])){
				$sql.= " AND kl.chutri in (".$search['chutri'].") ";
			}
			if(!empty($search['nvchuyenquan'])){
				$sql.= " AND kl.nvchuyenquan in (".$search['nvchuyenquan'].") ";
			}
			if(!empty($search['pheduyet'])){
				$sql.= " AND kl.pheduyet in (".$search['pheduyet'].") ";
			}	
			if(!empty($search['noidung'])){
				$sql.= " AND kl.noidung like '%".$search['noidung']."%' ";
			}		
		}
	    if($groups == 3){
			//$sql.= " AND kl.nvchuyenquan = '".$taikhoanid."' ";
            $sql.= " AND kl.nvchuyenquan LIKE '%,".$taikhoanid.",%'";
		}
		if($groups == 4 || $groups == 5 || $groups == 2){
			$sql.= " AND EXISTS(
					select pb.idkl
					from klphongban pb
					where pb.idphongban = '$donvi'
					and pb.idkl = kl.id
					limit 1
				)";
		}
		return $sql;
	}
	
	function getList1($search,$loai,$login){
		$groups = $login['groups'];
		$donvi = $login['donvi'];
		$taikhoan = $login['taikhoan'];
		$sql = "
				SELECT kl.*, u.fullname as nguoiketluan, u2.fullname as chutri, lgb.names as loaigiaoban, lkl.`names` as loaiketluan, 
u3.fullname as chuyenquan, pd.`names` as pheduyet
				FROM klgb kl
				LEFT JOIN users u on u.id = kl.nguoiketluan
				LEFT JOIN users u2 on u2.id = kl.chutri
				LEFT JOIN users u3 on u3.id = kl.nvchuyenquan
				LEFT JOIN loaigiaoban lgb on lgb.id = kl.loaigiaoban
				LEFT JOIN hinhthuckls lkl on lkl.id = kl.loaikl
				LEFT JOIN pheduyet pd on pd.id = kl.pheduyet
				WHERE kl.isdelete = 0
				";
		if(!empty($search['tungay'])){
			$sql.= " AND kl.ngaygiaoban >= '".date('Y-m-d',strtotime($search['tungay']))."' ";
		}
		if(!empty($search['denngay'])){
			$sql.= " AND kl.ngaygiaoban <= '".date('Y-m-d',strtotime($search['denngay']))."' ";
		}
		if(!empty($search['sovanban'])){
			$sql.= " AND kl.sovanban like '%".$search['sovanban']."%' ";
		}
		if(!empty($search['loaikl'])){
			$sql.= " AND kl.loaikl in (".$search['loaikl'].") ";
		}
		else{
			if($loai != ""){
				$sql.= " AND kl.loaikl = '".$loai."' ";
			}
		}
		if(!empty($search['chutri'])){
			$sql.= " AND kl.chutri in (".$search['chutri'].") ";
		}
		if(!empty($search['nvchuyenquan'])){
			$sql.= " AND kl.nvchuyenquan in (".$search['nvchuyenquan'].") ";
		}
		if(!empty($search['pheduyet'])){
			$sql.= " AND kl.pheduyet in (".$search['pheduyet'].") ";
		}
		if($groups == 3){
			$sql.= " AND kl.nguoitao = '".$taikhoan."' ";
		}
		if($groups == 4 || $groups == 5){
			$sql.= " AND EXISTS(
					select pb.idkl
					from klphongban pb
					where pb.idphongban = '$donvi'
					and pb.idkl = kl.id
					limit 1
				)";
		}
		return $sql;
	}
	function getTotal($search,$loai,$login){
		$groups = $login['groups'];
		$donvi = $login['donvi'];
		$taikhoan = $login['taikhoan'];
		$taikhoanid = $login['id'];
		$sql = "
				SELECT count(1) as total
				FROM klgb kl				
				LEFT JOIN loaigiaoban lgb on lgb.id = kl.loaigiaoban
				LEFT JOIN hinhthuckls lkl on lkl.id = kl.loaikl
				LEFT JOIN pheduyet pd on pd.id = kl.pheduyet
				WHERE kl.isdelete = 0
				";
		if(isset($search['sqlbutton']) && !empty($search['sqlbutton'])) {
			$where = $search['sqlbutton'];
			$pos = strpos($where, 'WHERE');
			if ($pos !== false)
				$sql .= " AND ".substr($where,$pos+5);
		} else {
			if(!empty($search['id'])){
				$sql.= " AND kl.id like '%".$search['id']."%' ";
			}
			//if($taikhoan!="admin")
				//$sql .= " AND kl.pheduyet = 0 and kl.tpduyet = 0";
			
			if(!empty($search['maklgb'])){
				$sql.= " AND kl.id = '".$search['maklgb']."' ";
			}
			if(!empty($search['tungay'])){
				$sql.= " AND kl.ngaygiaoban >= '".date('Y-m-d',strtotime($search['tungay']))."' ";
			}
			if(!empty($search['denngay'])){
				$sql.= " AND kl.ngaygiaoban <= '".date('Y-m-d',strtotime($search['denngay']))."' ";
			}
			if(!empty($search['sovanban'])){
				$sql.= " AND kl.sovanban like '%".$search['sovanban']."%' ";
			}
			if(!empty($search['loaikl'])){
				$sql.= " AND kl.loaikl in (".$search['loaikl'].") ";
			}
			else{
				if($loai != ""){
					$sql.= " AND kl.loaikl = '".$loai."' ";
				}
			}
			if(!empty($search['chutri'])){
				$sql.= " AND kl.chutri in (".$search['chutri'].") ";
			}
			if(!empty($search['nvchuyenquan'])){
				$sql.= " AND kl.nvchuyenquan in (".$search['nvchuyenquan'].") ";
			}
			if(!empty($search['pheduyet'])){
				$sql.= " AND kl.pheduyet in (".$search['pheduyet'].") ";
			}	
			if(!empty($search['noidung'])){
				$sql.= " AND kl.noidung like '%".$search['noidung']."%' ";
			}		
		}
	    if($groups == 3){
			//$sql.= " AND kl.nvchuyenquan = '".$taikhoanid."' ";
            $sql.= " AND kl.nvchuyenquan LIKE '%,".$taikhoanid.",%'";
		}
		if($groups == 4 || $groups == 5 || $groups == 2){
			$sql.= " AND EXISTS(
					select pb.idkl
					from klphongban pb
					where pb.idphongban = '$donvi'
					and pb.idkl = kl.id
					limit 1
				)";
		}
//		return $sql;
		$query = $this->model->query($sql)->execute();
		if(!empty($query[0]->total)){
			return $query[0]->total;
		}
		else{
			return 0;
		}
	}	
	function getTotal__($search,$loai,$login){
		$groups = $login['groups'];
		$donvi = $login['donvi'];
		$taikhoan = $login['taikhoan'];
		$sql = "
				SELECT0 count(1) total
				FROM klgb kl
				LEFT JOIN users u on u.id = kl.nguoiketluan
				LEFT JOIN users u2 on u2.id = kl.chutri
				LEFT JOIN users u3 on u3.id = kl.nvchuyenquan
				LEFT JOIN loaigiaoban lgb on lgb.id = kl.loaigiaoban
				LEFT JOIN hinhthuckls lkl on lkl.id = kl.loaikl
				LEFT JOIN pheduyet pd on pd.id = kl.pheduyet
				WHERE kl.isdelete = 0
				";
		if(!empty($search['maklgb'])){
				$sql.= " AND kl.id = '".$search['maklgb']."' ";
			}
		if(!empty($search['tungay'])){
			$sql.= " AND kl.ngaygiaoban >= '".date('Y-m-d',strtotime($search['tungay']))."' ";
		}
		if(!empty($search['denngay'])){
			$sql.= " AND kl.ngaygiaoban <= '".date('Y-m-d',strtotime($search['denngay']))."' ";
		}
		if(!empty($search['sovanban'])){
			$sql.= " AND kl.sovanban like '%".$search['sovanban']."%' ";
		}
		if(!empty($search['loaikl'])){
			$sql.= " AND kl.loaikl in (".$search['loaikl'].") ";
		}
		else{
			if($loai != ""){
				$sql.= " AND kl.loaikl = '".$loai."' ";
			}
		}
		if(!empty($search['chutri'])){
			$sql.= " AND kl.chutri in (".$search['chutri'].") ";
		}
		if(!empty($search['nvchuyenquan'])){
			$sql.= " AND kl.nvchuyenquan in (".$search['nvchuyenquan'].") ";
		}
		if(!empty($search['pheduyet'])){
			$sql.= " AND kl.pheduyet in (".$search['pheduyet'].") ";
		}
		if(!empty($search['noidung'])){
				$sql.= " AND kl.noidung like '%".$search['noidung']."%' ";
			}
		if($groups == 3){
			$sql.= " AND kl.nguoitao = '".$taikhoan."' ";
		}
		if($groups == 4 || $groups == 5){
			$sql.= " AND EXISTS(
					select pb.idkl
					from klphongban pb
					where pb.idphongban = '$donvi'
					and pb.idkl = kl.id
					limit 1
				)";
		}
		$query = $this->model->query($sql)->execute();
		if(!empty($query[0]->total)){
			return $query[0]->total;
		}
		else{
			return 0;
		}
	}
	function loaiketluan(){
		return $this->model->table('hinhthuckls')->where('isdelete',0)->find_combo('id','names');
	}
	function loaigiaoban(){
		return $this->model->table('loaigiaoban')->where('isdelete',0)->find_combo('id','names');
	}
	function listPhongban(){
		return $this->model->table('department')->where('isdelete',0)->order_by('orders','ASC')->find_combo('iduser','names');
	}
	function loaipheduyet(){
		return $this->model->table('pheduyet')->where('isdelete',0)->order_by('names','ASC')->find_combo('id','names');
	}
	function listChutri(){
		$sql = "
				select u.id, u.fullname
				from users u
				left join groups g on u.groups = g.id
				where u.isdelete = 0
				and g.types = 'GD';
		";
		$query = $this->model->query($sql)->execute();
		$arr = array();
		foreach($query as $item){
			$arr[$item->id] = $item->fullname;
		}
		return $arr;
	}
	function listChuyenquan(){
		$sql = "
				select u.id, u.fullname
				from users u
				left join groups g on u.groups = g.id
				where u.isdelete = 0
				and g.types = 'CQ';
		";
		$query = $this->model->query($sql)->execute();
		$arr = array();
		foreach($query as $item){
			$arr[$item->id] = $item->fullname;
		}
		return $arr;
	}	
	
	function saves($data,$phongban,$id,$objAdd){		
		$login = $this->pub->GetSession('login');
		$this->db->trans_start();
		//$objCq = $this->department_model->loadChuyenquan($data['nvchuyenquan']);
		//echo "<pre>"; print_r($objCq); exit();
        if(!empty($data['nvchuyenquan'])){
            //$objCq = $this->department_model->loadChuyenquans($data['nvchuyenquan']);
            $arrcq = $this->department_model->loadChuyenquans($data['nvchuyenquan']);
            //echo "<pre>"; print_r($arrcq); exit();
            //$data['machuyenquan'] = isset($objCq->ma)?$objCq->ma:"";
            //$data['tenchuyenquan'] = isset($objCq->ten)?$objCq->ten:"";
            $data['machuyenquan'] = $arrcq['ma'];
            $data['tenchuyenquan'] = $arrcq['ten'];
    		$data['nvchuyenquan'] = ",".$data['nvchuyenquan'].",";
    	}	
		if(empty($id)){			
			$data['ngaytao'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['nguoitao'] = $login['taikhoan']; 	
			$id = $this->model->save('',$data);
			//print_r($phongban); 
			//print_r($objAdd); 
			//exit;
			$this->saveDept($id,$phongban);
			$this->insertKLCon($id,$objAdd);
		}
		else{
			$data['ngaysua'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['nguoisua'] = $login['taikhoan'];	
			$this->model->where('id',$id)->update($data);
			$this->saveDept($id,$phongban);
			$this->saveDept2($id,$objAdd);
		}
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;	
		}
	}
	function insertKLCon($id,$objAdd){
		$login = $this->pub->GetSession('login');
		if($id != ""){
			if(count($objAdd) > 0){
				foreach($objAdd as $key=>$val){
					$arr = explode("_x_",$val);
					$array = array();
					if(!empty($arr[0]) && !empty($arr[1])){
						//$objDep = $this->department_model->loadDepartment($arr[1]);
						$objDep = $this->department_model->loadDepartmentSoap($arr[1]);
						$array['idkl'] = $id;
						$array['ngayhoanthanh'] = date('Y-m-d',strtotime($arr[2]));
						$array['idphongban'] = $arr[1];
						$array['madonvi'] = isset($objDep->usercode)?$objDep->usercode:"";
						$array['tendonvi'] = isset($objDep->names)?$objDep->names:"";
						$array['noidung'] = $arr[0];
						$array['types'] = 1;
						$array['usercreate'] = $login['taikhoan']; 	
						$array['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
						$this->model->table('klphongban')->insert($array);
					}		
				}
			}
		}
	}
	function saveDept2($id,$objAdd){
		$login = $this->pub->GetSession('login');
		if($id != ""){
			if(count($objAdd) > 0){
				//print_r($objAdd); exit();
				foreach($objAdd as $key=>$val){
					if($key>0) { //edit
						$arr = explode("_x_",$val);
						$array = array();
						if(!empty($arr[0]) && !empty($arr[1])){
							//$objDep = $this->department_model->loadDepartment($arr[1]);
							$objDep = $this->department_model->loadDepartmentSoap($arr[1]);
							$array['idkl'] = $id;
							$array['ngayhoanthanh'] = date('Y-m-d',strtotime($arr[2]));
							$array['idphongban'] = $arr[1];
							$array['madonvi'] = isset($objDep->usercode)?$objDep->usercode:"";
							$array['tendonvi'] = isset($objDep->names)?$objDep->names:"";
							$array['noidung'] = $arr[0];
							$array['types'] = 1;
							$array['usercreate'] = $login['taikhoan']; 	
							$array['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
							$this->model->table('klphongban')->where("id",$key)->update($array);
						}
					} else {
						$arr = explode("_x_",$val);
						$array = array();
						if(!empty($arr[0]) && !empty($arr[1])){
							//$objDep = $this->department_model->loadDepartment($arr[1]);
							$objDep = $this->department_model->loadDepartmentSoap($arr[1]);
							$array['idkl'] = $id;
							$array['ngayhoanthanh'] = date('Y-m-d',strtotime($arr[2]));
							$array['idphongban'] = $arr[1];
							$array['madonvi'] = isset($objDep->usercode)?$objDep->usercode:"";
							$array['tendonvi'] = isset($objDep->names)?$objDep->names:"";
							$array['noidung'] = $arr[0];
							$array['types'] = 1;
							$array['usercreate'] = $login['taikhoan']; 	
							$array['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
							$this->model->table('klphongban')->insert($array);
						}
					}					
				}
			}
		}
	}
	function saveDept($id,$phongban){
		if($id != "" && $phongban != ""){
			$login = $this->pub->GetSession('logins');
			$this->model->table('klphongban')
						->where('idkl',$id)
						->where('types',0)
						->delete();
			$arr = explode(",",$phongban);
			if(count($arr) > 0){
				foreach($arr as $key=>$idphongban){	
					//$objDep = $this->department_model->loadDepartment($idphongban);
					$objDep = $this->department_model->loadDepartmentSoap($idphongban);
					$array = array();
					$array['idkl'] = $id;
					$array['idphongban'] = $idphongban;
					$array['madonvi'] = isset($objDep->usercode)?$objDep->usercode:"";
					$array['tendonvi'] = isset($objDep->names)?$objDep->names:"";
					$array['types'] = 0;
					$array['usercreate'] = $login['taikhoan'];
					$array['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
					$this->model->table('klphongban')->insert($array);
				}
			}
		}
	}
	
	function getIDPhongban($id){
		$query = $this->model->table('klphongban')
						->where('idkl',$id)
						->where('types',0)
						->find_combo('idphongban','noidung');
		return $query;
	}
	
	function getIDPhongbanSub($id){
		$query = $this->model->table('klphongban')
						->where('idkl',$id)
						->where('types',1)
						->find_all();
		return $query;
	}
	function getIDPhongbanList($login){
		$pb = $login['donvi'];
		
		$sql = "SELECT id, idkl, noidung, tendonvi
					FROM klphongban
					where types = 1";
		if($login['groups'] == 4 || $login['groups'] == 5){
			$sql.= " and idphongban = '$pb'";
		}		
		$query = $this->model->query($sql)->execute();
		$arr = array();
		foreach($query as $item){
			$arr[$item->idkl][$item->id] = $item->tendonvi .': '.$item->noidung;
		}
		return $arr;
	}
	function getIDPhongbanListOld($login){
		$pb = $login['donvi'];
		
		$sql = "SELECT kl.id,kl.idkl , kl.noidung, d.`names` as dept 
					FROM klphongban kl
					left join department d on d.iduser = kl.idphongban
					where kl.types = 1";
		if($login['groups'] == 4 || $login['groups'] == 5){
			$sql.= " and kl.idphongban = '$pb'";
		}		
		$query = $this->model->query($sql)->execute();
		$arr = array();
		foreach($query as $item){
			$arr[$item->idkl][$item->id] = $item->noidung .'-'.$item->dept;
		}
		return $arr;
	}
	/*
	function deletes($str,$item){
		if(!empty($item)){
			$this->model->where('id',$item)->update(array('isdelete'=>1));
			echo 1;
		}
		else{
			$array = explode(',',$str);
			foreach($array as $k=>$id){
				$this->model->where('id',$id)->update(array('isdelete'=>1));	
				echo 1;
			}
		}
	}
	*/
	function deletes($id){
		if(!empty($id)){
			$this->model->where('id',$id)->update(array('isdelete'=>1));
			$this->model->table("klphongban")->where('idkl',$id)->update(array('isdelete'=>1));
			$this->model->table("chamdiem")->where('idklgb',$id)->update(array('isdelete'=>1));
			$this->model->table("traketqua")->where('idklgb',$id)->update(array('isdelete'=>1));
			$this->model->table("yctp")->where('idklgb',$id)->update(array('isdelete'=>1));
			$this->model->table("ycpkh")->where('idklgb',$id)->update(array('isdelete'=>1));
			return 1;
		}
		return 0;
	}
	
	function getyeucaupkh($idgiaoban){
		$ycpkh = $this->model->table('ycpkh')
							 ->select('id,idklgb,noidung')
							 ->where('idklgb in ('.$idgiaoban.')')
							 ->find_all();
		$arr = array();
		foreach($ycpkh as $item){
			$arr[$item->idklgb][$item->id] = $item->noidung;
		}
		return $arr;
	}
	function getPhongbanthuchien($idgiaoban){
		$sql = "
				SELECT kl.id,kl.idkl, kl.noidung, d.`names`
				FROM klphongban kl
				left join department d on d.iduser = kl.idphongban
				where kl.idkl in (".$idgiaoban.")
				;
		";
		$query = $this->model->query($sql)->execute();
		$arr_nd = array();
		$arr_pb = array();
		foreach($query as $item){
			$arr_nd[$item->idkl][$item->id] = $item->noidung;
		}
		foreach($query as $item){
			$arr_pb[$item->idkl][$item->id] = $item->names;
		}
		$array = array();
		$array['noidung'] = $arr_nd;
		$array['phongban'] = $arr_pb;
		return $array;
	}
	function listKetluancon($id){
		$klcon = "
					select kl.idkl, kl.noidung, kl.idphongban, d.`names` as phongban
					from klphongban kl
					left join department d on d.iduser = kl.idphongban
					where kl.idkl = '$id'";
					
		$query = $this->model->query($klcon)->execute();
		return $query;		 
	}
	
	function listKetluanconSoap($id){
		$arr = $this->model->table("klphongban")->select("idkl, noidung, idphongban, tendonvi as phongban")->where("idkl",$id)->find_all();
		// co the lay du lieu ten phong ban tu soap nhu sau
		/*
		foreach($arr as $item) {
			$sql="SELECT ma, ten FROM (`trungtam`) WHERE id=".$item->idphongban;
			$response=$this->pub->ketnoi_sql($sql,'get');
			$item->phongban = $response[0]['ten'];
		}
		*/
		//print_r($arr); exit();
		return $arr;		 
	}
	
	function idketluancon($id,$idphongban){
		$ketluancon = $this->model->table('klphongban')
								   ->select('id')
								   ->where('idkl',$id)	
								   ->where('idphongban',$idphongban)
								   ->order_by('id','DESC')
								   ->find();
		if(!empty($ketluancon->id)){
			return $ketluancon->id;
		}
		else{
			return 0;
		}
	}
	
	function combo_chuyenquan($login)
    {	
		/*
		if($login['taikhoan'] != 'admin') 
		{
            $combo_chuyenquan['id']=$login['taikhoan'].' - '.$login['ten'];
        }
        else
        {
            $sql="SELECT `id`, `tennhanvien` as ten, taikhoan FROM (`nhanvien`) where `phanmem` LIKE '%,".$this->pub->config['session_name'].",%' and trungtam='".$login['donvi']."' and tennhanvien<>'P.KH'" ;//lay nhan vien chuyen quan la phong ke hoach//print_r($sql);exit;
			$usersss=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
			$combo_chuyenquan=array();
			$combo_chuyenquan['0']='Chọn';
			for($i=0;$i<count($usersss);$i++)
			{
				$combo_chuyenquan[$usersss[$i]['id']]=$usersss[$i]['taikhoan'].' - '.$usersss[$i]['ten'];		   
			}
        }
		*/
		
		$sql="SELECT `id`, `tennhanvien` as ten, taikhoan FROM (`nhanvien`) where `phanmem` LIKE '%,".$this->pub->config['session_name'].",%' and trungtam='25' and tennhanvien<>'P.KH'" ;//lay nhan vien chuyen quan la phong ke hoach//print_r($sql);exit;
		$response=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
		$combo_chuyenquan=array();	
		for($i=0;$i<count($response);$i++)
		{
			$combo_chuyenquan[$response[$i]['id']]=$response[$i]['taikhoan'].' - '.$response[$i]['ten'];		   
		}       
		return $combo_chuyenquan;
    }
	
	function local_nhanvien()
    {
		$arrobj = $this->model->table("users")->where("isdelete",0)->find_all();		
		$list_nhanvien=array();	
		foreach($arrobj as $item)
		{
			$list_nhanvien[$item->id] = $item->fullname;
		}       
		return $list_nhanvien;
    }
	
	function list_nhanvien()
    {
		$sql="SELECT id, tennhanvien, taikhoan FROM (`nhanvien`) where thungrac=0" ;
		$response=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
		$list_nhanvien=array();	
		for($i=0;$i<count($response);$i++)
		{
			$list_nhanvien[$response[$i]['id']] = $response[$i]['tennhanvien'];   
		}       
		return $list_nhanvien;
    }
	
	function list_nhanvien_manv()
    {
		$sql="SELECT id, tennhanvien, taikhoan FROM (`nhanvien`) where thungrac=0" ;
		$response=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
		$list_nhanvien=array();
		for($i=0;$i<count($response);$i++)
		{
			$ma = strtolower($response[$i]['taikhoan']);
			$list_nhanvien[$ma]['id'] = $response[$i]['id'];
			$list_nhanvien[$ma]['tennhanvien'] = $response[$i]['tennhanvien'];		   
		}   
		return $list_nhanvien;
    }
	
	function combo_phongban($login)
    {
		/*
		if($login['taikhoan'] != 'admin') 
		{
            $combo_chuyenquan['id']=$login['taikhoan'].' - '.$login['ten'];
        }
        else
        {
            $sql="SELECT `id`, `tennhanvien` as ten, taikhoan FROM (`nhanvien`) where `phanmem` LIKE '%,".$this->pub->config['session_name'].",%' and trungtam='".$login['donvi']."' and tennhanvien<>'P.KH'" ;//lay nhan vien chuyen quan la phong ke hoach//print_r($sql);exit;
			$usersss=$this->pub->ketnoi_sql($sql,'get');//print_r($sql);exit;
			$combo_chuyenquan=array();
			$combo_chuyenquan['0']='Chọn';
			for($i=0;$i<count($usersss);$i++)
			{
				$combo_chuyenquan[$usersss[$i]['id']]=$usersss[$i]['taikhoan'].' - '.$usersss[$i]['ten'];		   
			}
        }
		*/
		
		$sql="SELECT id, ma, ten FROM (`trungtam`) where thungrac=0 ORDER BY thutu";
		$response=$this->pub->ketnoi_sql($sql,'get'); //echo "<pre>"; print_r($response); exit;
		$combo_phongban=array();		
		for($i=0;$i<count($response);$i++)
		{
			$combo_phongban[$response[$i]['id']]=$response[$i]['ma'].' - '.$response[$i]['ten'];		   
		}
		return $combo_phongban;
    }
	
	function arr_donvi_soap($login)
    {		
		$sql="SELECT id, ma, ten, loai FROM (`trungtam`) where thungrac=0 ORDER BY thutu";
		$response=$this->pub->ketnoi_sql($sql,'get'); //echo "<pre>"; print_r($response); exit;
		return $response;
    }
}